package common;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by J.Chen on 2017/5/31.
 */
public class AnalysisClient {

    private static AnalysisClient client = null;

    private static final Map<String, Class<? extends DataAnalysis>> dataAnalysis = new HashMap<>();

    public static AnalysisClient getClient() {
        if (client == null) {
            synchronized (AnalysisClient.class) {
                if (client == null) {
                    client = new AnalysisClient();
                    String packName = "analysis";
                    Set<Class<?>> set = ClassScan.getClasses(packName);
                    for (Class clazz : set) {
                        if (clazz != null && DataAnalysis.class.isAssignableFrom(clazz)) {
                            AnalysisWare analysisWare = (AnalysisWare) clazz.getAnnotation(AnalysisWare.class);
                            if (analysisWare != null && analysisWare.open()) {
                                if (dataAnalysis.containsKey(analysisWare.confCode())) {
                                    System.err.println(analysisWare.confCode() + " repeat!");
                                } else {
                                    dataAnalysis.put(analysisWare.confCode(), clazz);
                                }
                            }
                        }
                    }

                }
            }
        }
        return client;
    }

    public Class<DataAnalysis> getDataAnalysis(String confCode) {
        return (Class<DataAnalysis>) dataAnalysis.get(confCode);
    }

    public static void main(String[] args) {
        getClient();
        for (Map.Entry e : dataAnalysis.entrySet()) {
            System.out.println(e.getKey() + ":" + e.getValue());
        }
    }
}
